<?php
namespace app\business\controller;
use app\business\controller\Base;
use app\business\model\BusinessEnvironment;
use app\business\model\BusinessInfo;
use app\business\model\BusinessLogin;
use app\business\model\BusinessMenu;
use think\Controller;
use think\Db;
use think\Session;

class Seller extends controller
{
    public function index()
    {
        $data = input('get.');

        $users = db('login')->where('username',$data['name'])->find();

        if ($users) {
             if(password_verify($data['password'],$users['password'])){
               $d = db('info')->where('business_id',$users['business_id'])->find();
               if ($d['business_trial'] !== 2) {
                   $data['a'] = 'weitongguoshenhe';  //店铺未通过审核
               }

                 $data['a'] = 'ok'; //信息正确
                 $data['b'] = $d['id'];  // 门店ID
            }else{
                 $data['a'] = 'mimabuzhengque';  //密码不正确

            }
        }else{
             $data['a'] = 'yonghubucunzai';  //用户名不存在

        }

        return json_encode($data);

    }

//获取商家所有订单
    public function order_all()
    {
        $info_id = input('get.id');

        //定义空数组  以防下面无参数报错！
        $data = [];
//查询待接单订单 （已支付）
        $order = db('order')->where('static',input('get.static'))->where('is_comment',0)->where('order_status',1)->where('shop_id',$info_id)->order('id desc ')->page(input('get.page'),'4')->select();

        foreach ($order as $key => $value) {
            # code...
            $data['a'][$key] = $value;
            $data['a'][$key]['list_name'] = $this->list_name($value['id']);
            $data['a'][$key]['post_time'] = date('m-d H:s',$value['order_time']);
        }

//查询待配送订单（已支付）
        $order = db('order')->where('static',input('get.static'))->where('is_comment',1)->where('order_status',1)->where('shop_id',$info_id)->order('id desc ')->page(input('get.page'),'4')->select();

        foreach ($order as $key => $value) {
            # code...
            $data['b'][$key] = $value;
            $data['b'][$key]['list_name'] = $this->list_name($value['id']);
            $data['b'][$key]['post_time'] = date('m-d H:s',$value['order_time']);
        }
//查询已经退款订单（已支付）
        $order = db('order')->where('static',input('get.static'))->where('is_comment',5)->where('order_status',1)->where('shop_id',$info_id)->order('id desc ')->page(input('get.page'),'4')->select();

        foreach ($order as $key => $value) {
            # code...
            $data['c'][$key] = $value;
            $data['c'][$key]['list_name'] = $this->list_name($value['id']);
            $data['c'][$key]['post_time'] = date('m-d H:s',$value['order_time']);
        }
//查询已经完成的订单（已支付）
        $order = db('order')->where('static',input('get.static'))->where('is_comment',4)->where('order_status',1)->where('shop_id',$info_id)->order('id desc ')->page(input('get.page'),'4')->select();

        foreach ($order as $key => $value) {
            # code...
            $data['d'][$key] = $value;
            $data['d'][$key]['list_name'] = $this->list_name($value['id']);
            $data['d'][$key]['post_time'] = date('m-d H:s',$value['order_time']);
        }

        //var_dump($data);die;
        return json_encode($data);
    }

    public function order_all_diannei()
    {

        $info_id = input('get.id');
        //查询已支付店内订单 （已支付）
        $order = db('order')->where('static',1)->where('order_status',1)->where('shop_id',$info_id)->order('id desc ')->page(input('get.page'),'4')->select();

        foreach ($order as $key => $value) {
            # code...
            $data['a'][$key] = $value;
            $data['a'][$key]['list_name'] = $this->list_name($value['id']);
            $data['a'][$key]['user_name'] = $this->user_name($value['openId']);
            $data['a'][$key]['post_time'] = date('m-d H:s',$value['order_time']);
        }


         //查询未支付店内订单 （已支付）
        $order = db('order')->where('static',1)->where('order_status',2)->where('shop_id',$info_id)->order('id desc ')->page(input('get.page'),'4')->select();

        foreach ($order as $key => $value) {
            # code...
            $data['b'][$key] = $value;
            $data['b'][$key]['list_name'] = $this->list_name($value['id']);
            $data['b'][$key]['user_name'] = $this->user_name($value['openId']);
            $data['b'][$key]['post_time'] = date('m-d H:s',$value['order_time']);
        }

        //var_dump($data);die;
        return json_encode($data);
    }

//根据openid 返回用户名称  （字符串）
    private function user_name($openid)
    {
        $users = db('users')->where('openid',$openid)->find();

        return $users['nickname'];
    }
//根据订单ID 返回菜品名   （字符串）
    private function list_name($id)
    {
        $order_goods = db('order_goods')->where('oid',$id)->select();

        foreach ($order_goods as $key => $value) {
            # code...
                $data[$key] = $value['goods_name'];


        }

        return implode(",", $data);
    }
//将订单状态修改为配送中
    public function order_static()
    {
        $update['id'] = input('get.oid');
        $update['is_comment'] = 2;
        $static = db('order')->update($update);

        return $static;
    }

//商家后台首页数据获取
    public function gzt()
    {

        $time = mktime(0,0,0,date('m'),date('d'),date('Y'));

        $toptime = mktime(0,0,0,date('m')-1,date('d'),date('Y'));

        $info_id = input('get.info_id');

        $info = db('info')->find($info_id);



        //查找今日下单数量
        $data['a'] = db('order')->where('shop_id',$info_id)->where('order_time','>',$time)->where('is_comment','neq',5)->count();
        //查找今日已付款的下单数量
        $data['b'] = db('order')->where('shop_id',$info_id)->where('order_time','>',$time)->where('is_comment','neq',5)->where('order_status',1)->count();
        //查找今日已支付的所有订单
        $order = db('order')->where('shop_id',$info_id)->where('order_time','>',$time)->where('is_comment','neq',5)->where('order_status',1)->select();
        //今日收益
        $data['c'] = $this->order_money($order);
        //查找昨日订单
        $order = db('order')->where('shop_id',$info_id)->where('order_time','>',$toptime)->where('is_comment','neq',5)->where('order_time','<',$time)->where('order_status',1)->select();
        //昨日收益
        $data['d'] = $this->order_money($order);
        //查找所有已支付订单
        $order = db('order')->where('is_comment','neq',5)->where('shop_id',$info_id)->where('order_status',1)->select();
        //所有收益
        $data['e'] = $this->order_money($order);
        //店铺浏览量
        $data['f'] = $info['browsingvolume'];

        return json_encode($data);

    }

//获取营业额
    private function order_money($order)
    {
        $money = 0;

         foreach ($order as $key => $value) {
 
            $money += $value['allPayMoney'];

        }

        return $money;
    }
//商店编辑 获取商家级相关所有数据
    public function info()
    {
        $info_id = input('get.info_id');
        //商家基本信息
        $data['a'] = db('info')->find($info_id);
        //商家环境图片
        $data['b'] = db('environment')->where('business_id',$info_id)->select();

        return json_encode($data);
    }
//上传图片

    public function uploadFile($xia)
    {
        $file = request()->file('file');


        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {

            $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/shop');
            if ($info) {

                $nowDate = date('Ymd');
                $data['o'] = $xia;
                $data['b'] = $nowDate . '/' . $info->getFilename();

            }
        }


        return json_encode($data);
    }

//商家编辑数据库操作
    public function info_do_update()
    {
        $data = input('get.');

        //判断是否选择位置
        if ($data['business_lng'] == 0) {
            unset($data['business_lng']);
            unset($data['business_lat']);
        }

        if ($data['business_address'] == 0) {
            unset($data['business_address']);
        }

        //插入新增图片
        $insert = json_decode($data['cimg']);
        $einsert['business_id'] = $data['id'];
        unset($data['cimg']);
        //插入环境图片
        if ($insert) {

             foreach ($insert as $key => $value) {

                    $einsert['environment_img'] = $value;

                    db('environment')->insert($einsert);

                }
        }


        //删除图片
        $delete_img = json_decode($data['delete_img']);
            //var_dump($delete_img);die;
        if ($delete_img) {
            # code...

             db('environment')->delete($delete_img);
        }

        unset($data['delete_img']);

        //修改登录密码
        if (!$data['password'] == 0) {

            $login = db('login')->where('business_id',$data['id'])->find();

            $lwhere['id'] = $login['id'];

            $lwhere['password'] = password_hash($data['password'], PASSWORD_DEFAULT);

            db('login')->update($lwhere);


        }
        unset($data['password']);

        db::table('business_info')->update($data);

        return 1;
    }

    public function tixian()
    {
        $cash = db('cash')->where('business_id',input('get.info_id'))->find();

        return json_encode($cash);
    }

    public function do_tixian()
    {
        $data = input('get.');
        if ($data['cash_type'] !== '银行卡') {
            unset($data['back_name']);
        }
        $data['create_time'] = time();
        Db::table('business_cash_get')->insert($data);


        $info = Db::table('business_cash')->where('business_id',$data['business_id'])->find();

        $money = $info['cash_num']-$data['cash_money'];

        $static = Db::table('business_cash')->where('business_id',$data['business_id'])->update(['cash_num'=>$money]);

        return $static;
    }

    //获取商家所有菜品类型
    public function shop_type()
    {
    	$id = input('get.info_id');

    	$data = db('classify')->where('business_id',$id)->where('classify_delete',1)->select();

    	$arr = [];
    	// 数据库中存的名字   只取其中的名字即可
    	foreach ($data as $key => $value) {
    		$arr[$key]  = $value['classify_name'];
    	}

    	return json_encode($arr);

    }
	//获取商家所有菜品类型
    public function shoptype()
    {
    	$id = input('get.info_id');

    	$data = db('classify')->field('id,classify_name')->where('business_id',$id)->where('classify_delete',1)->select();

    	 

    	return json_encode($data);

    }
	public function shoptypeget()
    {
    	$id = intval(input('get.id'));
		$info_id = intval(input('get.info_id'));
		

    	$data = db('classify')->field('id,classify_name,classify_order')->where('business_id',$info_id)->where('classify_delete',1)->where('id',$id)->find();

    	 

    	return json_encode($data);

    }
	public function shoptypeadd(){
		$data = input('get.');
        if (empty($data['id'])) {
            if(Db::table('business_classify')->insert($data)){
				return 1;
			}
        }
		else{
			if(Db::table('business_classify')->where('business_id',$data['business_id'])->where('id',$data['id'])->update(['classify_name'=>$data['classify_name'],'classify_order'=>$data['classify_order']])){
				return 1;
			}
		}
        return 0;
	}
	public function shoptypedel(){
		$id = intval(input('get.id'));
		$info_id = intval(input('get.info_id'));
		if(Db::table('business_classify')->where('business_id',$info_id)->where('id',$id)->update(['classify_delete'=>0])){
			return 1;
		}
        return 0;
	}
	public function menu_add()
    {
    	$data = input('get.');
		if(Db::table('business_menu')->insert($data)){
			return 1;
		}
		return 0;
    }
	public function menu_edit()
    {
    	$data = input('get.');
		$business_id = intval(input('get.business_id'));
		$id = intval(input('get.id'));
		unset($data['id']);
		unset($data['business_id']);
		if(BusinessMenu::where('id',$id)->where('business_id',$business_id)->update($data)){
			return 1;
		}
		return 0;
    }
	public function shopedit()
    {
    	$business_id = intval(input('get.info_id'));
		$id = intval(input('get.id'));
		$data = BusinessMenu::where('business_id',$business_id)->where('menu_delete',1)->where('id',$id)->find();
		return json_encode($data);
    }

	public function shoplist()
    {
        $business_id = intval(input('get.business_id'));
		$good_type = intval(input('get.good_type'));
		if($good_type==0){
			$data = BusinessMenu::where('menu_delete',1)->order(['menu_order' => 'desc'])->where('business_id',$business_id)->select();
		}
		else{
			if($good_type==1){
				$menu_status = 1;
			}
			else{
				$menu_status = 0;
			}
			$data = BusinessMenu::where('menu_delete',1)->where('menu_status',$menu_status)->order(['menu_order' => 'desc'])->where('business_id',$business_id)->select();
		}
		return json_encode($data);
    }
	public function shopdel()
    {
        $business_id = intval(input('get.business_id'));
		$id = intval(input('get.good_id'));
		if(BusinessMenu::where('id',$id)->where('business_id',$business_id)->update(['menu_delete'=>0])){
			return 1;
		}
		return 0;
    }
	public function shopup()
    {
        $business_id = intval(input('get.business_id'));
		$id = intval(input('get.good_id'));
		if(BusinessMenu::where('id',$id)->where('business_id',$business_id)->update(['menu_status'=>1])){
			return 1;
		}
		return 0;
    }
	public function shopdown()
    {
        $business_id = intval(input('get.business_id'));
		$id = intval(input('get.good_id'));
		if(BusinessMenu::where('id',$id)->where('business_id',$business_id)->update(['menu_status'=>0])){
			return 1;
		}
		return 0;
    }
	
	//获得今日订单
	public function getDayAll()
	{
		        $info_id = input('get.id');

        //定义空数组  以防下面无参数报错！
        $data = [];
//查询待接单订单 （已支付）
        $order = db('order')->where('shop_id',$info_id)->where('order_time','>',mktime(0,0,0,date('m'),date('d'),date('Y')))->where('is_comment','neq',5)->order('id desc ')->select();
//本月内的订单
		//$order = db('order')->where('shop_id',$info_id)->where('order_time','>',mktime(0,0,0,date('m'),1,date('Y')))->where('order_time','<',(mktime(23,59,59,date('m'),date('t'),date('Y'))))->order('id desc ')->select();
        //$ordera=count($order);
		foreach ($order as $key => $value) {
            # code...
            $data['a'][$key] = $value;
            $data['a'][$key]['list_name'] = $this->list_name($value['id']);
            $data['a'][$key]['post_time'] = date('m-d H:s',$value['order_time']);
        }
		//data['orderacount']=$ordera;
		$order = db('order')->where('static',input('get.static'))->where('shop_id',$info_id)->where('is_comment','neq',5)->where('order_status',1)->where('order_time'>mktime(0,0,0,date('m'),date('d'),date('Y')))->where('order_time'<(mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1))->order('id desc ')->select();
        //$orderb=count($order);
        foreach ($order as $key => $value) {
            # code...
            $data['b'][$key] = $value;
            $data['b'][$key]['list_name'] = $this->list_name($value['id']);
            $data['b'][$key]['post_time'] = date('m-d H:s',$value['order_time']);
        }
		//$data['orderbcount']=$orderb;
		return json_encode($data);
	}

	//微信小程序获得预约订单
    public function yuyue()
    {
        $info_id = input('get.id');
        //var_dump($_SESSION);die;
        $data = db('yuyue')->where('shopid',$info_id)->page(input('get.page'),4)->select();

        return json_encode($data);
    }

    public function orderone()
    {
        $id=input('get.id');
        $data=Db::table('business_order')
            ->join('business_info',['business_order.shop_id=business_info.business_id'],'left')
            ->join('business_lessen',['business_order.shop_id=business_lessen.business_id','business_order.lessen_id=business_lessen.id'],'left')
            ->where('business_order.id',$id)
            ->find();
        if($data){
            $data['list_name']=$this->list_name($id);
            $data['post_time']=date('Y-m-d H:s',$data['order_time']);
            if(!$data['lessen_reduce']){
                $data['lessen_reduce']=0;
            }
        }

        return json_encode($data);
    }
}